//
//  ListNewsViewController.m
//  Doc Bao
//
//  Created by Hai Nguyen on 10/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ListNewsViewController.h"
#import "News.h"
#import "NewsExtensions.h"
#import "NewsCell.h"
#import "ListFeedViewController.h"

@implementation ListNewsViewController
@synthesize newsTable;
@synthesize listNews;
@synthesize newsCell;
@synthesize feedViewController;

#pragma mark -
#pragma mark UITableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [listNews count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 70.0f;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *newsCellId = @"NewsCellId";
	newsCell = (NewsCell*)[newsTable dequeueReusableCellWithIdentifier:newsCellId];
	if (newsCell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:self options:nil];
	}
	News *news = [listNews objectAtIndex:indexPath.row];
	newsCell.newsThumbnail.image = [UIImage imageNamed:news.Logo];
	[newsCell.newsNameLabel setText:news.Name];
	return [newsCell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	News *news = [listNews objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:feedViewController animated:YES];
	[feedViewController layoutForTheNews:news];
}

#pragma mark -

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	listNews = [[NSMutableArray alloc] init];
	[listNews setArray:[News getNews]];
	[newsTable reloadData];
	feedViewController = [[ListFeedViewController alloc] init];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.newsTable = nil;
	self.newsCell = nil;
}

- (void)dealloc {
	[newsTable release];
	[listNews release];
	[newsCell release];
	[feedViewController release];
    [super dealloc];
}


@end
